package com.redoor.app.stake.workflow.service.impl;

import com.redoor.app.stake.auth.service.BaseUserService;
import com.redoor.app.stake.basicdata.model.LogEnum;
import com.redoor.app.stake.common.utils.UUIDTool;
import com.redoor.app.stake.workflow.commons.WorkFlowStatus;
import com.redoor.app.stake.workflow.dao.BaseWorkorderOperationLogMapper;
import com.redoor.app.stake.workflow.model.BaseWorkorder;
import com.redoor.app.stake.workflow.model.BaseWorkorderOperationLog;
import com.redoor.app.stake.workflow.service.BaseWorkorderOperationLogService;
import com.redoornetwork.framework.mybatis.service.AbstractService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by wangjunlei on 2018-03-07 17:06:26.
 */
@Service
public class BaseWorkorderOperationLogServiceImpl extends AbstractService<BaseWorkorderOperationLog, String> implements BaseWorkorderOperationLogService {
    private static final Logger logger = LoggerFactory.getLogger(BaseWorkorderOperationLogServiceImpl.class);

    @Autowired
    private BaseWorkorderOperationLogMapper baseWorkorderOperationLogMapper;

    @Autowired
    private WorkOrderLogServiceImpl logService;

    @Autowired
    private BaseUserService baseUserService;

    @Override
    public int saveAll(List<BaseWorkorderOperationLog> list) {
        if (0 < baseWorkorderOperationLogMapper.batchInsert(list)){
            return 1;
        }else {
            return 0;
        }
    }

    @Override
    public boolean addTraceLog(BaseWorkorderOperationLog baseWorkorderOperationLog,BaseWorkorder baseWorkorder) {
        logger.info("BaseWorkorderOperationLogServiceImpl_addTraceLog_insert"+baseWorkorderOperationLog.getBeforstatus());
        baseWorkorderOperationLog.setUuid(UUIDTool.getUUID());
        baseWorkorderOperationLog.setWorkuuid(baseWorkorder.getUuid());
        baseWorkorderOperationLog.setOperatortime(new Date());
        baseWorkorderOperationLog.setOperatoruuid(baseUserService.getShiroUserUuid());
        baseWorkorderOperationLog.setRemark(baseWorkorder.getRemark());
        logger.info("BaseWorkorderOperationLogServiceImpl_addTraceLog_insertUuid"+baseWorkorderOperationLog.getUuid());
        return logService.addWorOrderLogToRedis(baseWorkorderOperationLog, LogEnum.WORKORDER_LIFE_LOG);
    }

    @Override
    public List<BaseWorkorderOperationLog> getWorkOrderLogList(String workOrderUuid) {
        return baseWorkorderOperationLogMapper.getWorkOrderLogList(workOrderUuid);
    }

    @Override
    public boolean reCreateLog(BaseWorkorder baseWorkorder) {
        BaseWorkorderOperationLog baseWorkorderOperationLog = new BaseWorkorderOperationLog();
        /*操作类型:注册码生成
         * 通过前:空
         * 通过后:待审核状态*/
        baseWorkorderOperationLog.setOpertortype(Integer.valueOf(WorkFlowStatus.WORKORDER_LIFE_REG_CREATE.getCode()));
        baseWorkorderOperationLog.setBeforstatus(WorkFlowStatus.WORKORDER_LIFE_REG_CREATE.getMessage());
        baseWorkorderOperationLog.setAfterstatus(WorkFlowStatus.WORKORDER_STATUS_APPLYING.getMessage());
        return this.addTraceLog(baseWorkorderOperationLog,baseWorkorder);
    }

    @Override
    public boolean regAcceptLog(BaseWorkorder baseWorkorder) {
        BaseWorkorderOperationLog baseWorkorderOperationLog = new BaseWorkorderOperationLog();
        /*操作类型:注册码审核通过
        * 通过前:审核状态
        * 通过后:关闭状态*/
        baseWorkorderOperationLog.setOpertortype(Integer.valueOf(WorkFlowStatus.WORKORDER_LIFE_REG_ACCEPT.getCode()));
        baseWorkorderOperationLog.setBeforstatus(WorkFlowStatus.WORKORDER_STATUS_APPLYING.getMessage());
        baseWorkorderOperationLog.setAfterstatus(WorkFlowStatus.WORKORDER_STATUS_FINISH.getMessage());
        return this.addTraceLog(baseWorkorderOperationLog,baseWorkorder);
    }

    @Override
    public boolean regRejectLog(BaseWorkorder baseWorkorder) {
        BaseWorkorderOperationLog baseWorkorderOperationLog = new BaseWorkorderOperationLog();
        /*操作类型:注册码审核拒绝
        * 拒绝前:审核状态
        * 拒绝后:拒绝状态*/
        baseWorkorderOperationLog.setOpertortype(Integer.valueOf(WorkFlowStatus.WORKORDER_LIFE_REG_REJECT.getCode()));
        baseWorkorderOperationLog.setBeforstatus(WorkFlowStatus.WORKORDER_STATUS_APPLYING.getMessage());
        baseWorkorderOperationLog.setAfterstatus(WorkFlowStatus.WORKORDER_STATUS_REJECT.getMessage());
        return this.addTraceLog(baseWorkorderOperationLog,baseWorkorder);
    }

    @Override
    public boolean maApplyCreateLog(BaseWorkorder baseWorkorder) {
        BaseWorkorderOperationLog baseWorkorderOperationLog = new BaseWorkorderOperationLog();
        /*操作类型:桩维护工单生成
         * 提交前:空
         * 提交后:待审核状态(待维护)*/
        baseWorkorderOperationLog.setOpertortype(Integer.valueOf(WorkFlowStatus.WORKORDER_LIFE_APPLY_CREATE.getCode()));
        baseWorkorderOperationLog.setBeforstatus(WorkFlowStatus.WORKORDER_LIFE_APPLY_CREATE.getMessage());
        baseWorkorderOperationLog.setAfterstatus(WorkFlowStatus.WORKORDER_STATUS_APPLYING.getMessage());
        return this.addTraceLog(baseWorkorderOperationLog,baseWorkorder);
    }

    @Override
    public boolean maApplySubmitLog(BaseWorkorder baseWorkorder) {
        BaseWorkorderOperationLog baseWorkorderOperationLog = new BaseWorkorderOperationLog();
        /*操作类型:桩维护工单提交
        * 提交前:审核状态(待审核)
        * 提交后:提交状态(通过)*/
        baseWorkorderOperationLog.setOpertortype(Integer.valueOf(WorkFlowStatus.WORKORDER_LIFE_APPLY_SUBMIT.getCode()));
        baseWorkorderOperationLog.setBeforstatus(WorkFlowStatus.WORKORDER_STATUS_APPLYING.getMessage());
        baseWorkorderOperationLog.setAfterstatus(WorkFlowStatus.WORKORDER_STATUS_ACCEPT.getMessage());
        return this.addTraceLog(baseWorkorderOperationLog,baseWorkorder);
    }

    @Override
    public boolean maApplyActiveLog(BaseWorkorder baseWorkorder) {
        BaseWorkorderOperationLog baseWorkorderOperationLog = new BaseWorkorderOperationLog();
        /*操作类型:维护工单激活
        * 激活前:提交状态(通过)
        * 激活后:审核状态(待审核)*/
        baseWorkorderOperationLog.setOpertortype(Integer.valueOf(WorkFlowStatus.WORKORDER_LIFE_APPLY_ACTIVE.getCode()));
        baseWorkorderOperationLog.setBeforstatus(WorkFlowStatus.WORKORDER_STATUS_APPLYING.getMessage());
        baseWorkorderOperationLog.setAfterstatus(WorkFlowStatus.WORKORDER_STATUS_ACCEPT.getMessage());
        return this.addTraceLog(baseWorkorderOperationLog,baseWorkorder);
    }

    @Override
    public boolean maApplyCloseLog(BaseWorkorder baseWorkorder) {
        BaseWorkorderOperationLog baseWorkorderOperationLog = new BaseWorkorderOperationLog();
        /*操作类型:维护工单关闭
        * 激活前:提交状态(通过)
        * 激活后:关闭状态(待审核)*/
        baseWorkorderOperationLog.setOpertortype(Integer.valueOf(WorkFlowStatus.WORKORDER_LIFE_APPLY_FINISH.getCode()));
        baseWorkorderOperationLog.setBeforstatus(WorkFlowStatus.WORKORDER_STATUS_ACCEPT.getMessage());
        baseWorkorderOperationLog.setAfterstatus(WorkFlowStatus.WORKORDER_STATUS_FINISH.getMessage());
        return this.addTraceLog(baseWorkorderOperationLog,baseWorkorder);
    }

    @Override
    public boolean maChargeCreateLog(BaseWorkorder baseWorkorder) {
        BaseWorkorderOperationLog baseWorkorderOperationLog = new BaseWorkorderOperationLog();
        /*操作类型:维护工单审核通过
         * 通过前:审核状态
         * 通过后:关闭状态*/
        baseWorkorderOperationLog.setOpertortype(Integer.valueOf(WorkFlowStatus.WORKORDER_LIFE_CHARGE_CREATE.getCode()));
        baseWorkorderOperationLog.setBeforstatus(WorkFlowStatus.WORKORDER_LIFE_CHARGE_CREATE.getMessage());
        baseWorkorderOperationLog.setAfterstatus(WorkFlowStatus.WORKORDER_STATUS_APPLYING.getMessage());
        return this.addTraceLog(baseWorkorderOperationLog,baseWorkorder);
    }

    @Override
    public boolean maChargeAcceptLog(BaseWorkorder baseWorkorder) {
        BaseWorkorderOperationLog baseWorkorderOperationLog = new BaseWorkorderOperationLog();
        /*操作类型:维护工单审核通过
        * 通过前:审核状态
        * 通过后:关闭状态*/
        baseWorkorderOperationLog.setOpertortype(Integer.valueOf(WorkFlowStatus.WORKORDER_LIFE_CHARGE_ACCEPT.getCode()));
        baseWorkorderOperationLog.setBeforstatus(WorkFlowStatus.WORKORDER_STATUS_APPLYING.getMessage());
        baseWorkorderOperationLog.setAfterstatus(WorkFlowStatus.WORKORDER_STATUS_FINISH.getMessage());
        return this.addTraceLog(baseWorkorderOperationLog,baseWorkorder);
    }

    @Override
    public boolean maChargeRejectLog(BaseWorkorder baseWorkorder) {
        BaseWorkorderOperationLog baseWorkorderOperationLog = new BaseWorkorderOperationLog();
        /*操作类型:维护工单审核拒绝
        * 拒绝前:审核状态
        * 拒绝后:拒绝状态*/
        baseWorkorderOperationLog.setOpertortype(Integer.valueOf(WorkFlowStatus.WORKORDER_LIFE_CHARGE_REJECT.getCode()));
        baseWorkorderOperationLog.setBeforstatus(WorkFlowStatus.WORKORDER_STATUS_APPLYING.getMessage());
        baseWorkorderOperationLog.setAfterstatus(WorkFlowStatus.WORKORDER_STATUS_REJECT.getMessage());
        return this.addTraceLog(baseWorkorderOperationLog,baseWorkorder);
    }

    @Override
    public boolean maChargeActiveLog(BaseWorkorder baseWorkorder) {
        BaseWorkorderOperationLog baseWorkorderOperationLog = new BaseWorkorderOperationLog();
        /*操作类型:维护审核工单激活
        * 激活前:拒绝状态
        * 激活后:待审核状态*/
        baseWorkorderOperationLog.setOpertortype(Integer.valueOf(WorkFlowStatus.WORKORDER_LIFE_CHARGE_ACTIVE.getCode()));
        baseWorkorderOperationLog.setBeforstatus(WorkFlowStatus.WORKORDER_STATUS_REJECT.getMessage());
        baseWorkorderOperationLog.setAfterstatus(WorkFlowStatus.WORKORDER_STATUS_APPLYING.getMessage());
        return this.addTraceLog(baseWorkorderOperationLog,baseWorkorder);
    }
}
